package Stack;

import java.util.Stack;

public class MinStack {
    Stack<Integer> minStack;
    Stack<Integer> stack;

    public MinStack() {
        this.minStack=new Stack<>();
        this.stack=new Stack<>();
    }

    public void push(int val) {
        if(stack.empty()){
            stack.push(val);
            minStack.push(val);
        }else{
            stack.push(val);
            if(stack.peek()<=minStack.peek()){//判断最小栈的元素是最小的
                int date=stack.peek();
                minStack.push(date);
            }
        }

    }

    public void pop() {
        int val = stack.pop();
        if(val == minStack.peek()) {
            minStack.pop();
        }
    }

    public int top() {
        return stack.peek();
    }

    public int getMin() {
        return minStack.peek();
    }
}
